package com.dianxin.smartpark.service.visitor.impl;

import java.util.Date;
import java.util.List;

import com.chinatelecom.response.common.CommonResponseBody;
import com.chinatelecom.response.utils.BeanUtil;
import com.chinatelecom.response.utils.Convert;
import com.chinatelecom.response.utils.ResponseBodyWrapperUtil;
import com.chinatelecom.response.constant.ErrMsgEnum;

import com.chinatelecom.response.utils.StringUtils;
import com.dianxin.smartpark.dao.dataobject.visitor.SmartParkVisitorSpecialUser;
import com.dianxin.smartpark.dao.dataobject.visitor.SmartParkVisitorSpecialUserExample;
import com.dianxin.smartpark.dao.mapper.visitor.SmartParkVisitorSpecialUserMapper;
import com.dianxin.smartpark.dao.qo.visitor.SmartParkVisitorSpecialUserQO;
import com.dianxin.smartpark.service.visitor.SmartParkVisitorSpecialUserService;
import com.dianxin.smartpark.service.vo.visitor.SmartParkVisitorSpecialUserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 特殊用户业务层处理
 * 
 * @author chenjiasong
 * @date 2021-08-04
 */
@Service
public class SmartParkVisitorSpecialUserServiceImpl implements SmartParkVisitorSpecialUserService
{
    private final static Logger logger = LoggerFactory.getLogger(SmartParkVisitorSpecialUserServiceImpl.class);
    @Resource
    private SmartParkVisitorSpecialUserMapper smartParkVisitorSpecialUserMapper;

    /**
     * 查询特殊用户
     * 
     * @param qo 特殊用户
     * @return 特殊用户
     */
    @Override
    public CommonResponseBody<SmartParkVisitorSpecialUserVO> selectSmartParkVisitorSpecialUser(SmartParkVisitorSpecialUserQO qo)
    {
        if(StringUtils.isNull(qo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkVisitorSpecialUserExample smartParkVisitorSpecialUserExample = new SmartParkVisitorSpecialUserExample();
        SmartParkVisitorSpecialUserExample.Criteria criteria = smartParkVisitorSpecialUserExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        criteria.andIsDeletedEqualTo(0);
        criteria.andIdEqualTo(qo.getId());
        List<SmartParkVisitorSpecialUser> passRecords = smartParkVisitorSpecialUserMapper.selectByExample(smartParkVisitorSpecialUserExample);
        return ResponseBodyWrapperUtil.successWrapper(StringUtils.isNotEmpty(passRecords)?passRecords.get(0):null,SmartParkVisitorSpecialUserVO.class);
    }

    /**
     * 查询特殊用户列表
     * 
     * @param qo 特殊用户
     * @return 特殊用户
     */
    @Override
    public CommonResponseBody<List<SmartParkVisitorSpecialUserVO>> selectSmartParkVisitorSpecialUserList(SmartParkVisitorSpecialUserQO qo)
    {
        SmartParkVisitorSpecialUserExample smartParkVisitorSpecialUserExample = new SmartParkVisitorSpecialUserExample();
        SmartParkVisitorSpecialUserExample.Criteria criteria = smartParkVisitorSpecialUserExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        if (qo.getUserType() != null) {
            criteria.andUserTypeEqualTo(qo.getUserType());
        }
        if (qo.getStatus() != null) {
            criteria.andStatusEqualTo(qo.getStatus());
        }
        if (qo.getUserName() != null) {
            criteria.andUserNameLike(qo.getUserName()+ "%");
        }

        criteria.andIsDeletedEqualTo(0);
        if (qo.getNeedPage()) {
            smartParkVisitorSpecialUserExample.setOrderByClause("gmt_create desc limit " + (qo.getPageIndex() - 1) * qo.getPageSize() + "," + qo.getPageSize());
        } else {
            smartParkVisitorSpecialUserExample.setOrderByClause("gmt_create desc");
        }
        List<SmartParkVisitorSpecialUser> passRecords = smartParkVisitorSpecialUserMapper.selectByExample(smartParkVisitorSpecialUserExample);
        long count = smartParkVisitorSpecialUserMapper.countByExample(smartParkVisitorSpecialUserExample);
        return ResponseBodyWrapperUtil
                .successListWrapper(passRecords, count, qo, SmartParkVisitorSpecialUserVO.class);
    }

    /**
     * 新增特殊用户
     * 
     * @param vo 特殊用户
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> insertSmartParkVisitorSpecialUser(@Valid SmartParkVisitorSpecialUserVO vo)
    {
        // 验证 todo
        SmartParkVisitorSpecialUser smartParkVisitorSpecialUser = new SmartParkVisitorSpecialUser();
        try {
            BeanUtil.insertConversion(vo, smartParkVisitorSpecialUser);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkVisitorSpecialUser.beforeInsert();
        return ResponseBodyWrapperUtil.toResData(smartParkVisitorSpecialUserMapper.insertSelective(smartParkVisitorSpecialUser));
    }

    /**
     * 修改特殊用户
     * 
     * @param vo 特殊用户
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> updateSmartParkVisitorSpecialUser(@Valid SmartParkVisitorSpecialUserVO vo)
    {
        // 验证 todo
        SmartParkVisitorSpecialUser smartParkVisitorSpecialUser = new SmartParkVisitorSpecialUser();
        try {
            BeanUtil.insertConversion(vo, smartParkVisitorSpecialUser);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkVisitorSpecialUser.beforeUpdate();
        if(StringUtils.isNotEmpty(vo.getIds())){
            if(vo.getIds().split(",").length>0){
                smartParkVisitorSpecialUser.setIds(Convert.toLongArray(vo.getIds()));
            }
        }
        return ResponseBodyWrapperUtil.toResData(smartParkVisitorSpecialUserMapper.updateByPrimaryKeySelective(smartParkVisitorSpecialUser));
    }

    /**
     * 删除特殊用户信息
     * 
     * @param vo 特殊用户
     * @return 结果08
     */
    @Override
    public CommonResponseBody<Boolean> deleteSmartParkVisitorSpecialUser(@Valid SmartParkVisitorSpecialUserVO vo)
    {
        // 验证 todo
        if(StringUtils.isNotEmpty(vo.getIds())){
            if(vo.getIds().split(",").length<=0){
                return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
            }
        }else {
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkVisitorSpecialUser smartParkVisitorSpecialUser = new SmartParkVisitorSpecialUser();
        try {
            BeanUtil.insertConversion(vo, smartParkVisitorSpecialUser);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkVisitorSpecialUser.beforeDelete();
        smartParkVisitorSpecialUser.setIds(Convert.toLongArray(vo.getIds()));
        return ResponseBodyWrapperUtil.toResData(smartParkVisitorSpecialUserMapper.updateByPrimaryKeySelective(smartParkVisitorSpecialUser));
    }
}
